package com.baizhi.controller;

import com.baizhi.entity.Employee;
import com.baizhi.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    @RequestMapping("queryAll")
    public String queryAll(HttpServletRequest request){

        List<Employee> employees=employeeService.queryAll();
        request.setAttribute("employees",employees);

        return "emplist";
    }

    //1、 批量删除
    @RequestMapping("plRemove")
    public String plRemove(HttpServletRequest request,int[] eids){


        if (eids!=null){
            employeeService.plDelete(eids);
        }
        return "redirect:/employee/findByPage";
    }


    //2、 分页查询
    @RequestMapping("findByPage")
    public String findByPage(HttpServletRequest request, Integer totalPage, Integer pageNumber){

        if (pageNumber == null) {
            pageNumber = 1;
        }

        ArrayList<Employee> employees = (ArrayList<Employee>) employeeService.queryByPage(pageNumber);
        request.setAttribute("employees",employees);
        request.setAttribute("pageNumber",pageNumber);

        //2、获取总页数
        totalPage = employeeService.getTotalPage();
        request.setAttribute("totalPage",totalPage);

        //跳转页面
        return "emplist";
    }


    //3、 移除
    @RequestMapping("remove")
    public String remove(Employee employee){

        employeeService.drop(employee.getEid());

        return "redirect:/employee/findByPage";
    }


    //4、 添加
    @RequestMapping("add")
    public String add(Employee employee){
        int i =1/0;
        employeeService.add(employee);

        return "redirect:/employee/findByPage";
    }


    //5、 查询一条员工数据
    @RequestMapping("queryone")
    public String queryone(Model request, Employee employee){

        employee = employeeService.queryOne(employee.getEid());
        System.out.println(employee);
        request.addAttribute("employee",employee);

        return "updateEmp";
    }

    //6、 更新
    @RequestMapping("update")
    public String update(Employee employee){

        employeeService.modify(employee);

        return "redirect:/employee/findByPage";
    }
}
